package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;


@RestController
@RequestMapping("user")
public class UserController {

    @Reference
    private UserService userService;

    /**
     * 用户名数据回显
     *
     * @param principal
     * @return
     */
    @RequestMapping("getUsername")
    public Result findUsername(Principal principal) {
        try {
            User user = userService.findUsername(principal.getName());
            return new Result(true, MessageConstant.GET_USERNAME_SUCCESS, user);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_USERNAME_FAIL);
        }
    }

    /**
     * 查询用户信息
     */
    @RequestMapping("findUser.do")
    public Result findUser(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult result = userService.queryUserByPage(queryPageBean);
            return new Result(true, "查询成功", result);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 添加用户
     */
    @RequestMapping("addUser.do")
    public Result addItem(@RequestParam("permissionIds") Integer[] permissionIds, @RequestParam("roleIds") Integer[] roleIds, @RequestBody User user) {

        try {
            userService.addUser(user, permissionIds, roleIds);
            return new Result(true, "添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    /**
     * 编辑勾选框数据回显
     */
    @RequestMapping("queryRoleIdsByUserId.do")
    public Result queryRoleIdsByUserId(@RequestParam("UserId") Integer userId) {
        try {
            Integer[] roleIds = userService.queryRoleIdsByUserId(userId);
            return new Result(true, "查询成功", roleIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 编辑勾选框数据回显
     */
    @RequestMapping("queryPermissionIdsByRoleId.do")
    public Result queryPermissionIdsByRoleId(@RequestParam("UserId") Integer userId) {
        try {
            Integer[] permissionIds = userService.queryPermissionIdsByRoleId(userId);
            return new Result(true, "查询成功", permissionIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /**
     * 用户更新
     */
    @RequestMapping("updateUsers.do")
    public Result updateUsers(@RequestParam("permissionIds") Integer[] permissionIds, @RequestParam("roleIds") Integer[] roleIds, @RequestBody User user) {

        try {
            userService.updateUsers(user, permissionIds, roleIds);
            return new Result(true, "更新成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "更新失败");
        }
    }

    /**
     * 删除用户
     *
     */

    @RequestMapping("deleteUserById.do")
    public Result deleteUserById(@RequestParam("UserId") Integer id) {
        try {
            userService.deleteUserById(id);
            return new Result(true, "删除成功");
        } catch (RuntimeException e) {
            e.printStackTrace();
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败");
        }
    }
}
